import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

// 职位等级
const POSITION_LEVELS = {
  LEVEL_1: '一品天官',
  LEVEL_2: '二品天官',
  LEVEL_3: '三品天官',
  LEVEL_4: '四品天官',
  LEVEL_5: '五品天官'
}

// 部门类型
const DEPARTMENT_TYPES = {
  LIFE_DEATH: '生死部',
  KARMA: '因果部',
  REINCARNATION: '轮回部',
  SALVATION: '超度部',
  JUDGMENT: '审判部',
  ARCHIVES: '档案部'
}

// 十王职责
const KINGS_DUTIES = {
  KING_1: {
    name: '秦广王',
    duty: '总管阴间事务，审查生死簿'
  },
  KING_2: {
    name: '楚江王',
    duty: '管理善恶功过'
  },
  KING_3: {
    name: '宋帝王',
    duty: '掌管轮回转世'
  },
  KING_4: {
    name: '仵官王',
    duty: '审判重大罪行'
  },
  KING_5: {
    name: '阎罗王',
    duty: '总裁生死'
  },
  KING_6: {
    name: '卞城王',
    duty: '管理地狱刑罚'
  },
  KING_7: {
    name: '泰山王',
    duty: '执掌善恶簿'
  },
  KING_8: {
    name: '都市王',
    duty: '管理城隍土地'
  },
  KING_9: {
    name: '平等王',
    duty: '审判生前罪过'
  },
  KING_10: {
    name: '转轮王',
    duty: '执行轮回转世'
  }
}

export const useManagementStore = defineStore('management', () => {
  // 状态
  const positions = ref([])
  const departments = ref([])
  const workflows = ref([])
  const kingsRules = ref([])

  // 计算属性
  const totalOfficials = computed(() => positions.value.length)
  const totalDepartments = computed(() => departments.value.length)

  // 职位管理方法
  const addPosition = async (position) => {
    try {
      // 模拟API调用
      const newPosition = {
        id: positions.value.length + 1,
        ...position
      }
      positions.value.push(newPosition)
      return newPosition
    } catch (error) {
      throw new Error('添加职位失败')
    }
  }

  const updatePosition = async (id, updates) => {
    try {
      const index = positions.value.findIndex((p) => p.id === id)
      if (index > -1) {
        positions.value[index] = { ...positions.value[index], ...updates }
        return positions.value[index]
      }
      throw new Error('职位不存在')
    } catch (error) {
      throw new Error('更新职位失败')
    }
  }

  const deletePosition = async (id) => {
    try {
      const index = positions.value.findIndex((p) => p.id === id)
      if (index > -1) {
        positions.value.splice(index, 1)
        return true
      }
      throw new Error('职位不存在')
    } catch (error) {
      throw new Error('删除职位失败')
    }
  }

  // 部门管理方法
  const addDepartment = async (department) => {
    try {
      const newDepartment = {
        id: departments.value.length + 1,
        ...department,
        upstreamDepartments: [],
        downstreamDepartments: []
      }
      departments.value.push(newDepartment)
      return newDepartment
    } catch (error) {
      throw new Error('添加部门失败')
    }
  }

  const updateDepartment = async (id, updates) => {
    try {
      const index = departments.value.findIndex((d) => d.id === id)
      if (index > -1) {
        departments.value[index] = { ...departments.value[index], ...updates }
        return departments.value[index]
      }
      throw new Error('部门不存在')
    } catch (error) {
      throw new Error('更新部门失败')
    }
  }

  const deleteDepartment = async (id) => {
    try {
      const index = departments.value.findIndex((d) => d.id === id)
      if (index > -1) {
        departments.value.splice(index, 1)
        return true
      }
      throw new Error('部门不存在')
    } catch (error) {
      throw new Error('删除部门失败')
    }
  }

  const updateDepartmentCollaboration = async (id, { upstreamDepartments, downstreamDepartments }) => {
    try {
      const index = departments.value.findIndex((d) => d.id === id)
      if (index > -1) {
        departments.value[index] = {
          ...departments.value[index],
          upstreamDepartments: departments.value.filter((d) => upstreamDepartments.includes(d.id)),
          downstreamDepartments: departments.value.filter((d) => downstreamDepartments.includes(d.id))
        }
        return departments.value[index]
      }
      throw new Error('部门不存在')
    } catch (error) {
      throw new Error('更新部门协作关系失败')
    }
  }

  // 十王分工管理方法
  const updateKingRules = async (kingId, updates) => {
    try {
      const king = KINGS_DUTIES[kingId]
      if (king) {
        KINGS_DUTIES[kingId] = { ...king, ...updates }
        return KINGS_DUTIES[kingId]
      }
      throw new Error('十王不存在')
    } catch (error) {
      throw new Error('更新十王职责失败')
    }
  }

  const addJudgmentRecord = async (kingId, record) => {
    try {
      const king = kingsRules.value.find((k) => k.id === kingId)
      if (king) {
        king.recentJudgments.unshift(record)
        return record
      }
      throw new Error('十王不存在')
    } catch (error) {
      throw new Error('添加判决记录失败')
    }
  }

  return {
    // 状态
    positions,
    departments,
    workflows,
    kingsRules,
    POSITION_LEVELS,
    DEPARTMENT_TYPES,
    KINGS_DUTIES,

    // 计算属性
    totalOfficials,
    totalDepartments,

    // 方法
    addPosition,
    updatePosition,
    deletePosition,
    addDepartment,
    updateDepartment,
    deleteDepartment,
    updateDepartmentCollaboration,
    updateKingRules,
    addJudgmentRecord
  }
})
